/**
 * 项目名：phms3
 * 日期：    2019-11-10 01:23:29
 * Copyright (c) 2015- HTSC-版权所有
 */

package com.joysuch.wwyt.core.repository;

import com.joysuch.wwyt.core.entity.BaseLoginAccount;
import com.joysuch.wwyt.core.entity.BaseUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Set;


/**
 * BaseLoginAccountDao
 */
public interface BaseLoginAccountDao
        extends Repository<BaseLoginAccount, Long>, QuerydslPredicateExecutor<BaseLoginAccount> {
    public Page<BaseLoginAccount> findAll(Specification<BaseLoginAccount> spec, Pageable pageable);

    public BaseLoginAccount findById(Long id);

    public BaseLoginAccount save(BaseLoginAccount bean);

    public void delete(BaseLoginAccount bean);

    public BaseLoginAccount findFirstByUserName(String userName);

    @Query(value = "select m from BaseLoginAccount m where userName =?1 and tenentId=?2 and orgCode like ?3%")
    public BaseLoginAccount findByNameAndTenentIdAndOrgCode(String userName, Long tenentId, String orgCode);

    // @Query("select b from BaseLoginAccount b where b.userName=?1 and b.orgCode like ?2%")
    public BaseLoginAccount findFirstByUserNameAndOrgCodeLike(String userName, String companyCode);

    public BaseLoginAccount findByBaseUser(BaseUser user);

    void deleteByBaseUser(BaseUser user);

    @Query("select count(m) from BaseLoginAccount m where userName=?1 and id != ?2")
    Long countOfUserNameExcludeId(String userName, Long id);

    @Modifying
    @Query(value = "UPDATE wwyt_base_login_account SET DELETE_FLAG ='1' WHERE USER_DETAIL_ID = ?1 ", nativeQuery = true)
    int deleteByUserId(Long id);

    @Modifying
    @Query(value = "UPDATE wwyt_base_login_account SET show_state = ?1 WHERE USER_DETAIL_ID = ?2 ", nativeQuery = true)
    void setAccountStateByUserId(Integer showState, Long userId);

    @Query(value = " SELECT * FROM  wwyt_base_login_account WHERE USER_DETAIL_ID= ?1 AND (show_state is null OR show_state = ?2)", nativeQuery = true)
    BaseLoginAccount findByBaseUserAndShowState(Long id, int i);

    @Query(value = " SELECT * FROM  wwyt_base_login_account WHERE USER_DETAIL_ID= ?1 ", nativeQuery = true)
    BaseLoginAccount findByUserDetailId(Long id);

    @Query(value = " SELECT * FROM  wwyt_base_login_account WHERE USER_NAME =?1 AND USER_DETAIL_ID!= ?2 AND DELETE_FLAG = '0'", nativeQuery = true)
    BaseLoginAccount findFirstByUserNameAndUserDetailIdNotEq(String account, Long id);

    @Transactional
    @Modifying
    @Query(value = "UPDATE wwyt_base_login_account SET DELETE_FLAG ='1' WHERE USER_DETAIL_ID in ?1 ", nativeQuery = true)
    void deleteByIds(List<Long> ids);

    @Query(value = " SELECT * FROM  wwyt_base_login_account WHERE USER_NAME= ?1 AND (show_state is null OR show_state != 44) AND ORG_CODE = ?2 AND TENENT_ID = ?3 ", nativeQuery = true)
    BaseLoginAccount findFirstByUserNameAndCurrentCompany(String userName, String orgCode, Long tenentId);
}
